﻿using Modding;
using Modding.Blocks;
using System;
using System.Collections.Generic;
using UnityEngine;


public class EnhancementBlock : MonoBehaviour
{
    public static bool EnhanceMore { get; internal set; } = false;

    /// <summary>模块行为</summary>
    public BlockBehaviour BB { get; internal set; }

   // public float AxisValue { get { return JoyAxis.getAxisValue(/*axis*/axisIndex); } internal set { AxisValue = value; } } 

    /// <summary>进阶属性按钮</summary>
    public MToggle Enhancement;
    public MToggle SignToggle;
    public MMenu JoystickMenu;

    public MSlider OffsetSlider;
    public MSlider MaxValueSlider;
    public MSlider MinValueSlider;

    public MToggle LerpToggle;
    public MSlider LerpSpeedSlider;

    //public float sign = 1f;
    ////public JoyAxis axis = JoyAxis.LeftHorizontalAxis;
    //public int axisIndex = 0;

    public JoyAxis joyAxis = JoyAxis.Default;

    /// <summary>进阶属性激活</summary>
    public bool EnhancementEnabled { get; set; } = false;

    public Action BlockPropertiseChangedEvent;

    private bool isFirstFrame = true;

    private void Awake()
    {

        BB = GetComponent<BlockBehaviour>();

        SafeAwake();

        Enhancement = BB.AddToggle(/*LanguageManager.Instance.CurrentLanguage.enhancement*//*"Enhancement"*/"Joystick", /*"Enhancement"*/"Joystick", EnhancementEnabled);
        Enhancement.Toggled += (bool value) => { EnhancementEnabled = value;/* DisplayInMapper(value);*/ SignToggle.DisplayInMapper = OffsetSlider.DisplayInMapper = MaxValueSlider.DisplayInMapper =MinValueSlider.DisplayInMapper  = LerpToggle.DisplayInMapper= LerpSpeedSlider.DisplayInMapper= JoystickMenu.DisplayInMapper = value; };

        SignToggle = BB.AddToggle("Sign", "Sign", /*Tool.FloatToBool(sign)*/joyAxis.Flipped);
        SignToggle.Toggled += (value) => { /*sign = Tool.BoolToFloat(value); */joyAxis.Flipped = value; };

        JoystickMenu = BB.AddMenu("JoystickMenu",/*Tool.JoyAxisTolist().IndexOf(axis.ToString())*/joyAxis.AxisIndex,/*Tool.JoyAxisTolist()*/new List<string> { "0","1","2","3","4","5","6","7"});
        JoystickMenu.ValueChanged += (value) => { /*axis = (JoyAxis)value;*/joyAxis.AxisIndex = value; };

        //OffsetSlider = BB.AddSlider("Offset", "Offset", joyAxis.Offset, -0.5f, 0.5f);
        //OffsetSlider.ValueChanged += (value) => { joyAxis.Offset = value; };

        MaxValueSlider = BB.AddSlider("Max Value", "Max Value", joyAxis.Max, 0f, 1f);
        MaxValueSlider.ValueChanged += (value) => { joyAxis.Max = value; };

        MinValueSlider = BB.AddSlider("Min Value", "Min Value", joyAxis.Min, -1f, 0f);
        MinValueSlider.ValueChanged += (value) => { joyAxis.Min = value; };

        LerpToggle = BB.AddToggle("Lerp Mode", "Lerp Mode", false);
        LerpToggle.Toggled += (value) => { LerpSpeedSlider.DisplayInMapper = value; };

        LerpSpeedSlider = BB.AddSlider("Lerp Speed", "Lerp Speed", 1f, 0.5f, 2f);

        // ChangedProperties(); try { BlockPropertiseChangedEvent.Invoke(); } catch { }

        //DisplayInMapper(EnhancementEnabled);

    }

    void Update()
    {
        if (BB.isSimulating)
        {
            if (isFirstFrame)
            {
                isFirstFrame = false;
                if (EnhancementEnabled) { OnSimulateStart_EnhancementEnabled(); }

                if (!StatMaster.isClient) { OnSimulateStartClient(); }
                OnSimulateStartAlways();
            }
            SimulateUpdateAlways();

            if (!EnhancementEnabled) return;

            if (StatMaster.isHosting) { SimulateUpdateHost_EnhancementEnabled(); }
            if (StatMaster.isClient) { SimulateUpdateClient_EnhancementEnabled(); }
            SimulateUpdateAlways_EnhancementEnable();
        }
        else
        {
            if (EnhancementEnabled) { BuildingUpdateAlways_EnhancementEnabled(); }
            isFirstFrame = true;
        }
    }

    private void FixedUpdate()
    {
        if (!EnhancementEnabled) return;

        if (BB.isSimulating && !isFirstFrame) { SimulateFixedUpdate_EnhancementEnabled(); }
    }

    private void LateUpdate()
    {
        if (!EnhancementEnabled) return;

        if (BB.isSimulating && !isFirstFrame) { SimulateLateUpdate_EnhancementEnabled(); }
    }


    /// <summary>
    /// 安全唤醒 模块只需要关心自己要添加什么控件就行了
    /// </summary>
    public virtual void SafeAwake() { }
    /// <summary>
    /// 在模拟开始的第一帧 要做的事
    /// </summary>
    public virtual void OnSimulateStartAlways() { }
    public virtual void OnSimulateStart_EnhancementEnabled() { }
    /// <summary>
    /// 在模拟模式下的Update
    /// </summary>
    public virtual void SimulateUpdateHost_EnhancementEnabled() { }
    /// <summary>
    /// 在模拟模式下的Update
    /// </summary>
    public virtual void SimulateUpdateClient_EnhancementEnabled() { }
    /// <summary>
    /// 在模拟模式下的Update
    /// </summary>
    public virtual void SimulateUpdateAlways_EnhancementEnable() { }
    public virtual void SimulateUpdateAlways() { }
    /// <summary>
    /// 在模拟模式下的FixedUpdate
    /// </summary>
    public virtual void SimulateFixedUpdate_EnhancementEnabled() { }
    /// <summary>
    /// 在模拟模式下的LateUpdate
    /// </summary>
    public virtual void SimulateLateUpdate_EnhancementEnabled() { }
    /// <summary>
    /// 建造模式下的Update
    /// </summary>
    public virtual void BuildingUpdateAlways_EnhancementEnabled() { }


    /// <summary>
    /// 显示在Mapper里面
    /// </summary>
    public virtual void DisplayInMapper(bool value) { }
    /// <summary>
    /// 属性改变（滑条值改变脚本属性随之改变）
    /// </summary>
    public virtual void ChangedProperties() { }
    /// <summary>
    /// 参数改变（联机模拟时主机对模块的一些参数初始化）
    /// </summary>
    public virtual void OnSimulateStartClient() { }




   
}




